//
//  ConnectionResizeView.m
//  DemoConnectionBetweenShapes
//
//  Created by duc nguyen van on 8/21/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "ConnectionResizeView.h"
#define EDGE_CHECK 20

@implementation ConnectionResizeView
@synthesize VOne,VTwo,VThree,VFour,VFive,VSix,VSeven,VEight,delegate;
#pragma mark -
#pragma mark some need methods

-(void)addCheckWith:(UIView*)check andCenter:(CGPoint)cen andTag:(int)tagC{
	check.bounds = CGRectMake(0, 0, EDGE_CHECK, EDGE_CHECK);
	check.backgroundColor = [UIColor redColor];
	check.center = cen;
	check.exclusiveTouch = YES;
	check.layer.cornerRadius = EDGE_CHECK/2;
	check.layer.borderColor = [[UIColor whiteColor] CGColor];
	check.layer.borderWidth = 2.0;
	check.layer.shadowRadius = 2;
    check.layer.shadowOpacity = 0.5;
	check.tag = tagC;
}

-(void)addConfigAllView{
	[self addCheckWith:VOne andCenter:CGPointMake(EDGE_CHECK/2, EDGE_CHECK/2) andTag:1];
	[self addCheckWith:VTwo andCenter:CGPointMake(self.frame.size.width / 2, EDGE_CHECK/2) andTag:2];
	[self addCheckWith:VThree andCenter:CGPointMake(self.frame.size.width- EDGE_CHECK/2, EDGE_CHECK/2) andTag:3];
	[self addCheckWith:VFour andCenter:CGPointMake(self.frame.size.width - EDGE_CHECK/2, self.frame.size.height /2) andTag:4];
	[self addCheckWith:VFive andCenter:CGPointMake(self.frame.size.width - EDGE_CHECK/2, self.frame.size.height - EDGE_CHECK/2) andTag:5];
	[self addCheckWith:VSix andCenter:CGPointMake(self.frame.size.width /2, self.frame.size.height - EDGE_CHECK/2) andTag:6];
	[self addCheckWith:VSeven andCenter:CGPointMake(EDGE_CHECK/2, self.frame.size.height - EDGE_CHECK/2) andTag:7];
	[self addCheckWith:VEight andCenter:CGPointMake(EDGE_CHECK/2, self.frame.size.height /2) andTag:8];	
}

-(void)addAllSubView{
	[self addSubview:VOne];
	[self addSubview:VTwo];
	[self addSubview:VThree];
	[self addSubview:VFour];
	[self addSubview:VFive];
	[self addSubview:VSix];
	[self addSubview:VSeven];
	[self addSubview:VEight];
}

-(void)removeSelectedSubView{
	selectedSubView.layer.borderColor = [[UIColor whiteColor] CGColor];
	selectedSubView.layer.borderWidth = 2.0;
}

-(void)createSelectedSubView:(UIView*)v{
	selectedSubView = v;
	selectedSubView.layer.borderColor = [[UIColor blueColor] CGColor];
	selectedSubView.layer.borderWidth = 5.0;
}

#pragma mark -
#pragma mark handel event

//tap
-(void)tapThisView:(UITapGestureRecognizer*)tap{
	[delegate tapOnView:tap];
}

-(void)tapViewOne:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VOne.layer.borderWidth = 5.0;
	VOne.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(EDGE_CHECK / 2, EDGE_CHECK / 2) andSubView:VOne];
	[self createSelectedSubView:VOne];
}

-(void)tapViewTwo:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VTwo.layer.borderWidth = 5.0;
	VTwo.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(self.frame.size.width / 2, EDGE_CHECK /2) andSubView:VTwo];
	[self createSelectedSubView:VTwo];
}

-(void)tapViewThree:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VThree.layer.borderWidth = 5.0;
	VThree.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(self.frame.size.width - EDGE_CHECK / 2, EDGE_CHECK / 2) andSubView:VThree];
	[self createSelectedSubView:VThree];
}

-(void)tapViewFour:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VFour.layer.borderWidth = 5.0;
	VFour.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(self.frame.size.width - EDGE_CHECK / 2, self.frame.size.height / 2) andSubView:VFour];
	[self createSelectedSubView:VFour];
}

-(void)tapViewFive:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VFive.layer.borderWidth = 5.0;
	VFive.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(self.frame.size.width - EDGE_CHECK / 2, self.frame.size.height - EDGE_CHECK /2) andSubView:VFive];
	[self createSelectedSubView:VFive];
}

-(void)tapViewSix:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VSix.layer.borderWidth = 5.0;
	VSix.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(self.frame.size.width / 2, self.frame.size.height - EDGE_CHECK /2) andSubView:VSix];
	[self createSelectedSubView:VSix];
}

-(void)tapViewSeven:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VSeven.layer.borderWidth = 5.0;
	VSeven.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(EDGE_CHECK / 2, self.frame.size.height - EDGE_CHECK / 2) andSubView:VSeven];
	[self createSelectedSubView:VSeven];
}

-(void)tapViewEight:(UITapGestureRecognizer*)tap{	
	[self removeSelectedSubView];
	VEight.layer.borderWidth = 5.0;
	VEight.layer.borderColor = [[UIColor blueColor] CGColor];
	[delegate tapOnSubView:tap andSize:CGSizeMake(EDGE_CHECK / 2, self.frame.size.height / 2) andSubView:VEight];
	[self createSelectedSubView:VEight];
	
	NSLog(@"Vone x %f y %f", VEight.center.x, VEight.center.y);
	CGPoint check = [VEight convertPoint:VEight.center toView:self.superview.superview];
	NSLog(@"Vone has change x %f y %f", check.x, check.y);
}

//pan
-(void)panMoveThisView:(UIPanGestureRecognizer*)pan{
	if([pan state] == UIGestureRecognizerStateBegan){
		BVexter = [pan locationInView:self];
	}
	
	MVexter = [pan locationInView:self];
	
	CGPoint vex;
	vex.x = MVexter.x - BVexter.x;
	vex.y = MVexter.y - BVexter.y;
	
	[delegate panOnThisView:pan andVexter:vex];

}

-(void)panMoveViewOne:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 1");
	[delegate moveWithPan:pan andID:1];
}

-(void)panMoveViewTwo:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 2");
	[delegate moveWithPan:pan andID:2];
}

-(void)panMoveViewThree:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 3");
}

-(void)panMoveViewFour:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 4");
}

-(void)panMoveViewFive:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 5");
}

-(void)panMoveViewSix:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 6");
}

-(void)panMoveViewSeven:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 7");
}

-(void)panMoveViewEight:(UIPanGestureRecognizer*)pan{	
	NSLog(@"move 8");
}

-(void)addPanGestureRecognizerForView:(UIView*)v andMethod:(SEL)method{
	UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:method];
	pan.delegate = self;
	[v addGestureRecognizer:pan];
	[pan release];
}

-(void)addTapGestureRecognizerForView:(UIView*)v andMethod:(SEL)method{
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:method];
	tap.delegate = self;
	[v addGestureRecognizer:tap];
	[tap release];
}

-(void)addGestureRecognizerFotAllView{
	[self addPanGestureRecognizerForView:self andMethod:@selector(panMoveThisView:)];
	
	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapThisView:)];
	tap.delegate = self;
	[self addGestureRecognizer:tap];
	[tap release];
	
	UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewOne:)];
	tapOne.delegate = self;
	[VOne addGestureRecognizer:tapOne];
	[tapOne release];
	
	//add tap gesture recognizer
	[self addTapGestureRecognizerForView:self andMethod:@selector(tapThisView:)];
	[self addTapGestureRecognizerForView:VOne andMethod:@selector(tapViewOne:)];
	[self addTapGestureRecognizerForView:VTwo andMethod:@selector(tapViewTwo:)];
	[self addTapGestureRecognizerForView:VThree andMethod:@selector(tapViewThree:)];
	[self addTapGestureRecognizerForView:VFour andMethod:@selector(tapViewFour:)];
	[self addTapGestureRecognizerForView:VFive andMethod:@selector(tapViewFive:)];
	[self addTapGestureRecognizerForView:VSix andMethod:@selector(tapViewSix:)];
	[self addTapGestureRecognizerForView:VSeven andMethod:@selector(tapViewSeven:)];
	[self addTapGestureRecognizerForView:VEight andMethod:@selector(tapViewEight:)];
	
	//add pan gesture recognizer
	[self addPanGestureRecognizerForView:self andMethod:@selector(panMoveThisView:)];
	[self addPanGestureRecognizerForView:VOne andMethod:@selector(panMoveViewOne:)];
	[self addPanGestureRecognizerForView:VTwo andMethod:@selector(panMoveViewTwo:)];
	[self addPanGestureRecognizerForView:VThree andMethod:@selector(panMoveViewThree:)];
	[self addPanGestureRecognizerForView:VFour andMethod:@selector(panMoveViewFour:)];
	[self addPanGestureRecognizerForView:VFive andMethod:@selector(panMoveViewFive:)];
	[self addPanGestureRecognizerForView:VSix andMethod:@selector(panMoveViewSix:)];
	[self addPanGestureRecognizerForView:VSeven andMethod:@selector(panMoveViewSeven:)];
	[self addPanGestureRecognizerForView:VEight andMethod:@selector(panMoveViewEight:)];
}

#pragma mark -
#pragma mark init

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		VOne = [[UIView alloc] init];
		VTwo = [[UIView alloc] init];
		VThree = [[UIView alloc] init];
		VFour = [[UIView alloc] init];
		VFive = [[UIView alloc] init];
		VSix = [[UIView alloc] init];
		VSeven = [[UIView alloc] init];
		VEight = [[UIView alloc] init];
		
		[self addConfigAllView];
		[self addAllSubView];
		[self addGestureRecognizerFotAllView];
		
		//release
		[VOne release];
		[VTwo release];
		[VThree release];
		[VFour release];
		[VFive release];
		[VSix release];
		[VSeven release];
		[VEight release];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
}


@end
